package Dungeoneering.Resources;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;

import Dungeoneering.Entities.MonsterDefenition;
import Dungeoneering.Entities.MonsterDefenition.AttackStyle;
import Dungeoneering.Entities.MonsterDefenition.Prayer;

/**
 * The ResourceCache will act as a mini database for monsterDefinition data. We read it from a txt file, and put it in our database so we can later find
 * the definition of a monster in here
 * @author Nicolaas
 *
 */
public class ResourceCache {
	private static final ResourceCache INSTANCE = new ResourceCache();
	private Hashtable<Integer, MonsterDefenition> monsters;

	/**
	 * private constructor creates instance of ResourceCache
	 */
	private ResourceCache() {
		monsters = new Hashtable<Integer, MonsterDefenition>();
		initialise();
	}
	
	private void initialise() {
		// TODO : read that generalnpcId, for every npc make a Monster object, and put it in the hashtable with its id as key
		try {
			BufferedReader listOfNpcs = new BufferedReader(new FileReader("generalnpcids.txt"));
			String s;
		
			while((s = (listOfNpcs.readLine())) != null) {							
				if(s.startsWith("//"))
					break;
				
				String[] stringArray = s.split(";");			
				
				int IDBound1 = Integer.parseInt(stringArray[1]);
				int IDBound2 = Integer.parseInt(stringArray[2]);
				Prayer Pray = Prayer.valueOf(stringArray[3]);
				AttackStyle Style1 = AttackStyle.valueOf(stringArray[4]);
				AttackStyle Style2 = AttackStyle.valueOf(stringArray[5]);
			
				for( int i = IDBound1; i <= IDBound2; i++) {
					monsters.put(i, new MonsterDefenition(i, Pray, Style1, Style2));
				}
			}
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			}
	}
	
	/**
	 * we don't use a synchronised method because it is up to 100 times slower...
	 * @return the resource cache singleton
	 */
	public static ResourceCache getInstance() {
		return INSTANCE;
	}
	
	public MonsterDefenition getMonster(int id) {
		return monsters.get(id);
	}
	
	
}
